package xyz.yolin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.yolin.pojo.Books;
import xyz.yolin.pojo.User;
import xyz.yolin.service.BookService;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;

    //查询全部书籍，返回页面
    @RequestMapping("/allBook")
    public String list(Model model){
        List<Books> books = bookService.queryAllBook();
        model.addAttribute("list",books);
        return "allBooks";
    }
    //跳转到增加书籍页面
    @RequestMapping("/toAddBook")
    public String toAddPaper(){
        return "addBook";
    }
    //添加书籍的请求
    @RequestMapping("/addBook")
    public String addBook(Books books){
        System.out.println(books);
        bookService.addBook(books);
        return "redirect:/book/allBook";
    }
    //跳转到修改页面
    @RequestMapping("updateBook")
    public String toUpdatePaper(int id,Model model){
        Books books = bookService.queryBook(id);
        model.addAttribute("book",books);
        return "updateBook";
    }
    //修改书籍
    @RequestMapping("updateBookMsg")
    public String updateBookMsg(Books book){
        int i = bookService.updateBook(book);
        if (i>0){
            System.out.println("数据修改成功");
        }
        return "redirect:/book/allBook";
    }
    //删除书籍
    @RequestMapping("/todeleteBook")
    public String deleteBook(int id){
        System.out.println(id);
        int i = bookService.deleteBook(id);
        return "redirect:/book/allBook";
    }
    //查询书籍
    @RequestMapping("/queryBook")
    public String queryBook(String queryBookName,Model model){
        List<Books> books = bookService.queryBookByName(queryBookName);
        if(books.size()==0){
            System.out.println("没有相关书籍");
            model.addAttribute("err","没有相关书籍");
        }
        model.addAttribute("list",books);
        return "allBooks";
    }
    @RequestMapping("/tologin")
    public String tologin(){
        return "login";
    }
    //判断用户登录
    @RequestMapping("/logininfo")
    public String logininfo(HttpSession session,User user, Model model){
        System.out.println("用户："+user);
        User user1 = bookService.checkLogin(user.getUsername());
        System.out.println("数据库："+user1);
        if (user1!=null&&user1.getPassword().equals(user.getPassword())){
            //把用户信息存在session中
            session.setAttribute("userInfo",user);
            return "redirect:/book/allBook";
        }else {
            model.addAttribute("msg","密码错误，登录失败,请重新登陆");
        }
        return "login";
    }

    //ajax
    @RequestMapping("/login")
    @ResponseBody
    public String login(String username,String password){
        System.out.println(username);
        String name=username;
        String pwd=password;
        String msg="";
        System.out.println(name.length());
        if(name.length()<=3||name.length()>10){
            return "账号输入有误，应为4~10位";
        }

        User user1 = bookService.checkLogin(name);


        if(name!=null){
            if(user1!=null){
                return "OK";
            }else{
                return "用户不存在";
            }
        }

        return msg;
    }
}
